package cn.test.app.a.server;

import cn.ifloat.brick.basic.common.toolkit.StringUtils;
import cn.test.app.a.service.OrderService;
import cn.test.app.a.api.OrderApi;
import cn.test.app.a.domain.Ord;
import cn.test.app.a.vo.OrdVo;
import com.baomidou.mybatisplus.core.conditions.interfaces.Func;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
public class OrderServer implements OrderApi {
    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private OrderService orderService;

    @PostConstruct
    public void init(){
        System.err.println("OrderServer   init");
    }

    /**
     * 新增订单详情
     * @param ord 订单详情
     */
    @Override
    public void add(@RequestBody Ord ord) {
        Long id = this.orderService.sequenceId();
        ord.setId(id);
        ord.setCreateTime(new Date());
        boolean save = orderService.save(ord);
        System.err.println("save = " + save);
    }

    /**
     * 删除订单
     * @param id 订单ID
     * @return 结果
     */
    @Override
    public Boolean del(@PathVariable("id") Long id) {
        return orderService.removeById(id);
    }

    /**
     * 修改订单信息
     * @param ord 订单详情
     * @return 结果
     */
    @Override
    public Boolean edit(@RequestBody Ord ord) {
        ord.setUpdateTime(new Date());
        return orderService.updateById(ord);
    }

    /**
     * 条件查询订单列表
     * @return 列表
     */
    @Override
    public List<Ord> list(@RequestBody OrdVo ordVo) {
        List<String> timeInterval = ordVo.getTimeInterval();
        QueryWrapper<Ord> wrapper = new QueryWrapper<>();
        if (!Objects.equals(timeInterval, null)) {
            //时间区间查询
            wrapper.ge(StringUtils.isNotEmpty(timeInterval.get(0)), "create_time", timeInterval.get(0)).
                    le(StringUtils.isNotEmpty(timeInterval.get(1)), "create_time", timeInterval.get(1));
        }

        //name不为空时，组装模糊查询条件
        wrapper.eq(!Objects.equals(ordVo.getId(), null), "id", ordVo.getId());
        wrapper.eq(!Objects.equals(ordVo.getUserId(), null), "user_id", ordVo.getUserId());
        wrapper.like(StringUtils.isNotBlank(ordVo.getUserName()), "user_name", ordVo.getUserName());
        wrapper.eq(!Objects.equals(ordVo.getGender(), null), "gender", ordVo.getGender());
        wrapper.eq(!Objects.equals(ordVo.getPhone(), null), "phone", ordVo.getPhone());
        wrapper.eq(!Objects.equals(ordVo.getGender(), null), "email", ordVo.getGender());
        wrapper.eq(!Objects.equals(ordVo.getIdCard(), null), "id_card", ordVo.getIdCard());
        List<Ord> list = orderService.list(wrapper);
        logger.info("list:{}", list.size());
        return list;
    }

    /**
     * 查询订单列表
     * @return 结果
     */
    @Override
    public List<Ord> ordList() {
        List<Ord> list = orderService.list();
        Collections.reverse(list);//反转
        logger.info("list:{}", list.size());
        return list;
    }

}
